home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MetalInternalFrameTitlePane.java < prev    next >
Text File  |  1998-06-30  |  13KB  |  403 lines

  1. /*
  2.  * @(#)MetalInternalFrameTitlePane.java    1.17 98/04/10
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.metal;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.border.*;
  27. import com.sun.java.swing.event.InternalFrameEvent;
  28. import java.util.EventListener;
  29. import java.beans.PropertyChangeListener;
  30. import java.beans.PropertyChangeEvent;
  31. import java.beans.VetoableChangeListener;
  32. import java.beans.PropertyVetoException;
  33.  
  34.  
  35.  
  36. /**
  37.  * Package private class that manages a JLF title bar
  38.  * @version 1.17 04/10/98
  39.  * @author Steve Wilson
  40.  */
  41. // Could not extend BasicInternalFrameTitlePane because it's private
  42. class MetalInternalFrameTitlePane extends JComponent 
  43.                                 implements LayoutManager, 
  44.                        ActionListener, 
  45.                        PropertyChangeListener {
  46.  
  47.   protected JMenuBar menuBar;
  48.   protected boolean isPalette = false;                        
  49.   JInternalFrame frame;
  50.   // the three control buttons
  51.   protected JButton iconButton;
  52.   protected JButton maxButton;
  53.   protected JButton closeButton;
  54.  
  55.   private static final Border handyEmptyBorder = new EmptyBorder(0,0,0,0);
  56.  
  57.   // the pictures/icons in these three buttons
  58.   Icon maxIcon; 
  59.   Icon altMaxIcon; 
  60.   Icon iconIcon; 
  61.   Icon closeIcon;
  62.  
  63.   int riseWidth = 0;    
  64.   int interButtonSpacing = 3;
  65.     
  66.   MetalBumps activeBumps = new MetalBumps( 0, 0,
  67.                        MetalLookAndFeel.getPrimaryControlHighlight(),
  68.                        MetalLookAndFeel.getPrimaryControlDarkShadow(),
  69.                        MetalLookAndFeel.getPrimaryControl() );
  70.   MetalBumps inactiveBumps = new MetalBumps( 0, 0,
  71.                          MetalLookAndFeel.getControlHighlight(),
  72.                          MetalLookAndFeel.getControlDarkShadow(),
  73.                          MetalLookAndFeel.getControl() );
  74.                         
  75.   public MetalInternalFrameTitlePane(JInternalFrame f) {
  76.     frame = f;
  77.  
  78.     setFont( UIManager.getFont("InternalFrameTitlePane.font") );
  79.     maxIcon = (Icon)UIManager.get("InternalFrameTitlePane.maximizeIcon");
  80.     altMaxIcon = (Icon)UIManager.get("InternalFrameTitlePane.altMaximizeIcon");
  81.     iconIcon = (Icon)UIManager.get("InternalFrameTitlePane.iconizeIcon");
  82.     closeIcon = (Icon)UIManager.get("InternalFrameTitlePane.closeIcon");
  83.  
  84. /*    menuBar = new JMenuBar(){
  85.       public boolean isFocusTraversable() { return false; }
  86.       public void requestFocus() {}
  87.       // PENDING(klobad) Should be able to configure Menu + Button instead
  88.       public void paint(Graphics g) {}
  89.       public boolean isOpaque() { return false; }
  90.     };
  91.     
  92.     menuBar.setBorderPainted(false);*/
  93.     
  94.     iconButton = new JButton( iconIcon );
  95.     iconButton.putClientProperty("paintActive", Boolean.TRUE);
  96.     iconButton.setFocusPainted(false);
  97.     iconButton.setBorder(handyEmptyBorder);
  98.     iconButton.setOpaque(false);
  99.     iconButton.addActionListener(this);
  100.     iconButton.setActionCommand("Iconify");
  101.     
  102.  
  103.     maxButton = new JButton( maxIcon );
  104.     maxButton.putClientProperty("paintActive", Boolean.TRUE);
  105.     maxButton.setBorder(handyEmptyBorder);
  106.     maxButton.setOpaque(false);
  107.     maxButton.setFocusPainted(false);
  108.     maxButton.addActionListener(this);
  109.     maxButton.setActionCommand("Maximize");
  110.     
  111.  
  112.     closeButton = new JButton( closeIcon );
  113.     closeButton.putClientProperty("paintActive", Boolean.TRUE);
  114.     closeButton.setBorder(handyEmptyBorder);
  115.     closeButton.setOpaque(false);
  116.     closeButton.setFocusPainted(false);     
  117.     closeButton.addActionListener(this);
  118.     closeButton.setActionCommand("Close");
  119.     
  120.     setLayout(this);
  121.     
  122.     //    add(menuBar);
  123.     add(iconButton);
  124.     add(maxButton);
  125.     add(closeButton);             
  126.     
  127.     // Make sure these are ok to leave on?
  128.     frame.addPropertyChangeListener(this);
  129.  
  130.   }
  131.                          
  132.  
  133.  
  134.   public void paint(Graphics g)  {
  135.     boolean isSelected = frame.isSelected();
  136.  
  137.     int width = getWidth();
  138.     int height = getHeight();
  139.     
  140.     Color background;
  141.     Color foreground;
  142.     Color shadow;
  143.     Color darkShadow;
  144.     Color highlight;
  145.  
  146.     MetalBumps bumps;
  147.  
  148.     Color fillColor = ((JInternalFrame)getParent()).getContentPane().getBackground();
  149.  
  150.     if (isSelected) {
  151.         background = MetalLookAndFeel.getWindowTitleBackground();
  152.         foreground = MetalLookAndFeel.getWindowTitleForeground();
  153.     shadow = MetalLookAndFeel.getPrimaryControlShadow();
  154.     darkShadow = MetalLookAndFeel.getPrimaryControlDarkShadow();
  155.     highlight = MetalLookAndFeel.getPrimaryControlHighlight();
  156.     bumps = activeBumps;
  157.     } else {
  158.         background = MetalLookAndFeel.getWindowTitleInactiveBackground();
  159.         foreground = MetalLookAndFeel.getWindowTitleInactiveForeground();
  160.            shadow = MetalLookAndFeel.getControlShadow();
  161.     darkShadow = MetalLookAndFeel.getControlDarkShadow();
  162.     highlight = MetalLookAndFeel.getControlHighlight();
  163.     bumps = inactiveBumps;
  164.     }
  165.  
  166.  
  167.  
  168.     g.setColor(background);
  169.     g.fillRect(0, 0, width, height);
  170.  
  171.     g.setColor( highlight );
  172.     g.drawRect( 0,0, width -1, width -1 );
  173.     g.fillRect( 0,0, 2, 2 );
  174.  
  175.     g.setColor( shadow );
  176.     g.drawLine ( 1, height - 2, width - 2, height -2);
  177.     g.drawLine ( width -1 , 1, width - 1, height -2);
  178.  
  179.     g.setColor( darkShadow );
  180.     g.drawLine ( 0, height - 1, width, height -1);
  181.     g.drawLine ( 0, 0, 0 ,0);    
  182.     g.drawLine ( width - 1, 0 , width -1, 0);
  183.  
  184.     int titleLength = 0;
  185.     int xOffset = 5;
  186.  
  187.     if(frame.getTitle() != null) {
  188.       Font f = getFont();
  189.       g.setFont(f);
  190.       FontMetrics fm = g.getFontMetrics();
  191.       int fHeight = fm.getHeight();
  192.  
  193.       Icon icon = frame.getFrameIcon();
  194.       if ( icon != null ) {
  195.     int iconY = ((height / 2) - (icon.getIconHeight() /2));
  196.     icon.paintIcon(frame, g, xOffset, iconY);
  197.         xOffset += icon.getIconWidth()+2;
  198.       }
  199.  
  200.       g.setColor(foreground);
  201.  
  202.       int yOffset = ( (height - fm.getHeight() ) / 2 ) + fm.getAscent();
  203.       g.drawString(frame.getTitle(),
  204.            xOffset,
  205.            yOffset );
  206.  
  207.       titleLength = fm.stringWidth(frame.getTitle());
  208.     }
  209.  
  210.     xOffset += titleLength + interButtonSpacing;
  211.     int bumpLength = getWidth() - (xOffset + riseWidth + interButtonSpacing );
  212.     int bumpHeight = getHeight() - (2 * interButtonSpacing) - 2;
  213.     bumps.setBumpArea( bumpLength, bumpHeight );
  214.     bumps.paintIcon( this, g, xOffset, interButtonSpacing);
  215.  
  216.     if (riseWidth != 0) {
  217.         xOffset += ( bumpLength - 1 );
  218.  
  219.     g.setColor( background); // erase bumps to make angle
  220.     g.fillRect( xOffset - 1, height - 7 ,  7 , 3);
  221.  
  222.     g.setColor( darkShadow );
  223.     g.drawLine ( xOffset, height - 2, xOffset  + 1, height - 2);
  224.     g.drawLine ( xOffset+2, height - 3, xOffset  + 2, height - 3);
  225.     g.drawLine ( xOffset+3, height - 4, xOffset  + 4, height - 4);
  226.     g.drawLine ( xOffset+5, height - 5, width, height - 5);
  227.  
  228.     g.setColor( shadow );
  229.     g.drawLine ( xOffset - 1, height - 3, xOffset  + 1, height - 3);
  230.     g.drawLine ( xOffset+1, height - 4, xOffset  + 2, height - 4);
  231.     g.drawLine ( xOffset+2, height - 5, xOffset  + 4, height - 5);
  232.     g.drawLine ( xOffset+4, height - 6, width, height - 6);
  233.  
  234.     g.setColor( fillColor );
  235.     g.drawLine ( xOffset, height -1, width, height - 1);
  236.     g.drawLine ( xOffset+2, height - 2, width, height - 2);
  237.     g.drawLine ( xOffset+3, height - 3, width, height - 3);
  238.     g.drawLine ( xOffset+5, height - 4, width, height - 4);
  239.  
  240.  
  241.     }
  242.  
  243.     paintChildren(g); 
  244.   }
  245.                          
  246.     /**
  247.      * Post a WINDOW_CLOSING-like event to the frame, so that it can
  248.      * be treated like a regular Frame.
  249.      */
  250.     void postClosingEvent(JInternalFrame frame) {
  251.         InternalFrameEvent e = new InternalFrameEvent(
  252.             frame, InternalFrameEvent.INTERNAL_FRAME_CLOSING);
  253.         // Try posting event, unless there's a SecurityManager.
  254.         if (JInternalFrame.class.getClassLoader() == null) {
  255.             try {
  256.                 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e);
  257.                 return;
  258.             } catch (SecurityException se) {
  259.                 // Use dispatchEvent instead.
  260.             }
  261.         }
  262.         frame.dispatchEvent(e);
  263.     }
  264.  
  265.     public void actionPerformed(ActionEvent e) {
  266.       if("Close".equals(e.getActionCommand()) && frame.isClosable())
  267.           postClosingEvent(frame);
  268.       else if("Iconify".equals(e.getActionCommand()) && frame.isIconifiable()) {
  269.     if(!frame.isIcon())
  270.       try { frame.setIcon(true); } catch (PropertyVetoException e1) { }
  271.     else
  272.       try { frame.setIcon(false); } catch (PropertyVetoException e1) { }
  273.  
  274.       ButtonModel model = iconButton.getModel();
  275.       if ( model != null ) {
  276.           model.setRollover( false );
  277.       }
  278.       } else if("Minimize".equals(e.getActionCommand()) && frame.isMaximizable()) {
  279.     try { frame.setIcon(true); } catch (PropertyVetoException e2) { }
  280.       } else if("Maximize".equals(e.getActionCommand()) && frame.isMaximizable()) {
  281.     if(!frame.isMaximum()) {
  282.       try { frame.setMaximum(true); } catch (PropertyVetoException e5) { }
  283.     } else {
  284.       try { frame.setMaximum(false); } catch (PropertyVetoException e6) { }
  285.     }
  286.       } else if("Restore".equals(e.getActionCommand()) && 
  287.                 frame.isMaximizable() && frame.isMaximum()) {
  288.     try { frame.setMaximum(false); } catch (PropertyVetoException e4) { }
  289.       } else if("Restore".equals(e.getActionCommand()) && 
  290.                 frame.isIconifiable() && frame.isIcon()) {
  291.     try { frame.setIcon(false); } catch (PropertyVetoException e4) { }
  292.       }
  293.     }
  294.                          
  295.     public void propertyChange(PropertyChangeEvent evt) {
  296.       String prop = (String)evt.getPropertyName();
  297.       JInternalFrame f = (JInternalFrame)evt.getSource();
  298.       boolean value = false;
  299.  
  300.       if ( frame.isSelected() ) {
  301.       iconButton.putClientProperty("paintActive", Boolean.TRUE);
  302.       closeButton.putClientProperty("paintActive", Boolean.TRUE);
  303.       maxButton.putClientProperty("paintActive", Boolean.TRUE);
  304.       repaint();
  305.       } else {
  306.       iconButton.putClientProperty("paintActive", Boolean.FALSE);
  307.       closeButton.putClientProperty("paintActive", Boolean.FALSE);
  308.       maxButton.putClientProperty("paintActive", Boolean.FALSE);
  309.       repaint();
  310.       }
  311.  
  312.       if(JInternalFrame.IS_SELECTED_PROPERTY.equals(prop)) {
  313.       repaint();
  314.       } else if(JInternalFrame.IS_MAXIMUM_PROPERTY.equals(prop)) {
  315.     value = ((Boolean)evt.getNewValue()).booleanValue();
  316.     if(value)
  317.              maxButton.setIcon(altMaxIcon);
  318.     else
  319.       maxButton.setIcon(maxIcon);
  320.       } else if(JInternalFrame.IS_ICON_PROPERTY.equals(prop)) {
  321.     value = ((Boolean)evt.getNewValue()).booleanValue();
  322.     if(value)
  323.       iconButton.setIcon(iconIcon);
  324.     else
  325.       iconButton.setIcon(iconIcon);  
  326.       } 
  327.     }
  328.     
  329.     public void addLayoutComponent(String name, Component c) {}
  330.     public void removeLayoutComponent(Component c) {}   
  331.  
  332.    public Dimension preferredLayoutSize(Container c)  {
  333.        return getPreferredSize(c);
  334.     }
  335.  
  336.  
  337.     public Dimension getPreferredSize(Container c)  {
  338.       return new Dimension(c.getSize().width, computeHeight());
  339.     }
  340.  
  341.     public Dimension minimumLayoutSize(Container c) {
  342.       return preferredLayoutSize(c);
  343.     } 
  344.     
  345.     protected int computeHeight() {
  346.       FontMetrics fm =  Toolkit.getDefaultToolkit().getFontMetrics(getFont());
  347.       int fontHeight = fm.getHeight();
  348.       fontHeight += 6;
  349.       int iconHeight = frame.getFrameIcon().getIconHeight();
  350.       iconHeight += 7;
  351.       
  352.       int finalHeight = Math.max( fontHeight, iconHeight );
  353.  
  354.       return finalHeight;
  355.     }    
  356.                     
  357.     public void layoutContainer(Container c) {
  358.  
  359.       int w = getWidth();
  360.       
  361.       // These should really be derived from the size of the Icons in the buttons
  362.       int buttonHeight = closeButton.getIcon().getIconHeight();  // this includes the border
  363.       int buttonWidth = closeButton.getIcon().getIconWidth();  // this includes the border
  364.  
  365.  
  366.       int x = (w) ;
  367.       
  368.       if(frame.isClosable()) {
  369.     x -= (buttonWidth+interButtonSpacing);
  370.     closeButton.setBounds(x , interButtonSpacing, buttonWidth, buttonHeight);
  371.       } else if(closeButton.getParent() != null) {
  372.     closeButton.getParent().remove(closeButton);
  373.       }
  374.       
  375.       if(frame.isMaximizable()) {
  376.     x -= (buttonWidth+interButtonSpacing);
  377.     maxButton.setBounds(x, interButtonSpacing, buttonWidth, buttonHeight);
  378.       } else if(maxButton.getParent() != null) {
  379.     maxButton.getParent().remove(maxButton);
  380.       }
  381.  
  382.       if(frame.isIconifiable()) {
  383.     x -= (buttonWidth+interButtonSpacing);
  384.     iconButton.setBounds(x, interButtonSpacing, buttonWidth, buttonHeight);
  385.  
  386.       } else if(iconButton.getParent() != null) {
  387.     iconButton.getParent().remove(iconButton);
  388.       }
  389.  
  390.       riseWidth = w-x;
  391.  
  392.       
  393.     } 
  394.  
  395.  
  396.     public void setPalette(boolean b) {
  397.         isPalette = b;
  398.     repaint();
  399.     }
  400.  
  401.                  
  402. };    /// End Title Pane Class
  403.